knn_impute Subroutine

public subroutine knn_impute(x, xc, k, weighted)

Imputes missing values by means of a k-nearest neighbors approach.

For each observation containing a missing value, the distance to every other observation is computed using only those variables observed in both rows. The distance is a standardized, root-mean-square difference such that rows sharing differing numbers of observed variables remain comparable. Each missing entry is then replaced by the mean of the corresponding entries of the k nearest rows for which that variable is observed. If no suitable donor exists, the variable mean, computed from the observed values, is used.

Arguments

Type IntentOptional Attributes Name
real(kind=real64), intent(in), dimension(:,:) :: x

An N-by-M matrix containing N observations of M variables. Missing entries must be denoted by NaN's (see missing_value).

real(kind=real64), intent(out), dimension(:,:) :: xc

An N-by-M matrix where the completed data set will be written.

integer(kind=int32), intent(in), optional :: k

An optional input specifying the number of neighbors to use. The default is 5.

logical, intent(in), optional :: weighted

An optional input that, if set to true, weights each donor by the inverse of its distance. The default is true.